<template>
    <div>
        <div class="sousuo">
            <input type="text" v-model="keyword" placeholder="请输入您想查看的课程">
            <a href="">搜索</a>
        </div>
        <div class="kecheng" v-show="keyword" ref="search">
            <ul>
                <li v-for="item of list" :key="item.id" class="border-bottom">
                    {{item.name}}
                </li>
                <li class="border-bottom" v-show="haslist">没有找到匹配数据</li>
            </ul>
        </div>
    </div>
</template>

<script>
import Bscroll from "better-scroll";
export default {
  name: "sousuo",
  props: {
    hotCities: Array
  },
  data() {
    return {
      keyword: "",
      list: [],
      timer: null
    };
  },
  watch: {
    keyword() {
      if (this.timer) {
        clearTimeout(this.timer);
      }
      if (!this.keyword) {
        this.list = [];
        return;
      }
      this.timer = setTimeout(() => {
        const result = [];
        //console.log(this.hotCities[i]);
        this.hotCities.forEach(val => {
          if (
            val.spell.toLowerCase().indexOf(this.keyword.toLowerCase()) > -1 ||
            val.name.indexOf(this.keyword) > -1
          ) {
            result.push(val);
            // console.log(val);
          }
        });
        this.list = result;
        // console.log(this.list);
      }, 100);
    }
  },
  mounted() {
    // console.log(this.$refs.search);
    this.scroll = new Bscroll(this.$refs.search);
    // console.log(this.hotCities);
  },
  computed: {
    haslist() {
      return !this.list.length;
    }
  }
};
</script>

<style scoped>
.sousuo {
  background: #f84849;
  padding: 1rem;
  padding-left: 1.2rem;
}
.sousuo input {
  width: 80%;
  height: 2rem;
  border-radius: 20px;
  box-sizing: border-box;
  padding-left: 3rem;
  background: #fff url(/static/images/sousuotubiao.png) no-repeat 0.5rem center;
  background-size: 8%;
}
.sousuo a {
  font-size: 1.2rem;
  color: #fff;
  display: inline-block;
  padding-left: 1rem;
  vertical-align: middle;
}
.kecheng {
  position: absolute;
  top: 7.5rem;
  overflow: hidden;
  left: 0;
  bottom: 4.5rem;
  right: 0;
  background: #fff;
  z-index: 1;
}
.kecheng ul li {
  padding-left: 2rem;
  height: 2.5rem;
  line-height: 2.5rem;
  font-size: 1rem;
  color: #666;
}
</style>